#!usr/bin/env python
# coding:utf-8
import json

class Config(object):
    """Config load from json file
    """

    def __init__(self, config=None, config_file=None):
        if config_file:
            with open(config_file, 'r',encoding="utf-8") as fin:
                config = json.load(fin)

        self.dict = config
        if config:
            self._update(config)

    def __getitem__(self, key):
        return self.dict[key]

    def __contains__(self, item):
        return item in self.dict

    def items(self):
        return self.dict.items()

    def add(self, key, value):
        """Add key value pair
        """
        self.__dict__[key] = value

    def _update(self, config):
        if not isinstance(config, dict):
            return

        for key in config:
            if isinstance(config[key], dict):
                config[key] = Config(config[key])

            if isinstance(config[key], list):
                config[key] = [Config(x) if isinstance(x, dict) else x for x in
                               config[key]]

        self.__dict__.update(config)


# def testarg(a=0,b=1,c=1):
#     print(a,b,c)

# if __name__ == '__main__':
#     import os
    # print(config.fasttext)
#     cur=os.path.dirname(os.path.dirname(__file__))
#     config=Config(config_file=os.path.join(cur,"extra_apps/nlp/conf.json"))
#     # print(type(config))
#     # print(config.fasttext.items())
#     # print(*config.fasttext.items())
#     print(config.fasttext_.dict)
#     print(type(config.fasttext_.items()))
#     print(dir(config.fasttext_))
    # print(type(config.fasttext_param.dict))
    # cc=**config.fasttext_
    # ** 参数可以少，但是不能多和重复
    # testarg(b=2, **config.fasttext_.dict,a=1)
    # testarg(a=1,**config.fasttext_param.dict)
